﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Automation.Protocols.Dao;
using Automation.Protocols.Model;
using Common.Logging;
using SupportLibrary;
using SupportLibrary.Message;

namespace Automation.Dal.Excel.Dao
{
    public class TestCommandDao : TestCommandBase
    {
        private ILog log = LogManager.GetLogger(typeof(TestCommandDao));
        private ExcelProvider _exProvider;
        private string _Path;
        public TestCommandDao(string path)
        {
            _Path = path;
            _exProvider = ExcelProvider.Create(@path);
            if (_exProvider == null)
                //MessageShow.FileDoesNotExist(path);
                throw new Exception(path + "doesnot exist!");
        }

        public override List<TestCommand> getTestcommands()
        {
            List<TestCommand> list = new List<TestCommand>();
            log.Debug(Resources.Information.msg_ReadingSheetTCommand);
            
            try
            {
                if (_exProvider != null)
                {
                    var TestCommands = _exProvider.GetSheet<Automation.Dal.Excel.Model.TestCommand>();
                    int i = 0;
                    if (TestCommands != null)
                    {
                        foreach (var item in TestCommands)
                        {
                            if (item.ID != null)
                            {
                                i++;
                                if (item.isValid())
                                {
                                    int n = list.Count;
                                    for (int j = 0; j < n; j++)
                                    {
                                        if (list[j].Keyword.Equals(item.Keyword))
                                        {
                                            //MessageShow.KeywordExist(item.Keyword);                                            
                                            //return null;
                                            throw new Exception("Keyword " + item.Keyword + " doesnot exist!");
                                        }
                                    }
                                    list.Add(new TestCommand()
                                    {
                                        ID = item.ID,
                                        Function = item.Function,
                                        Keyword = item.Keyword,
                                        Object = item.Object,
                                        Target = item.Target
                                    });
                                }
                                else
                                {
                                    //MessageShow.InvalidRecord(i + 1);
                                    throw new Exception("Record " + (i + 1) + " is invalid!");
                                }
                            }
                        }
                    }
                    
                    log.Debug(Message.ReplaceMessage(Resources.Information.msg_NumofRecord, ByKey.ByNumber(), i.ToString()));

                }
            }
            catch (Exception e)
            {
                //MessageShow.ExceptionOccur("Read TCommand sheet exception.", e);
                log.Error(Resources.ErrorMessage.ReadSheetErr + e.Message);
                throw new Exception(Resources.ErrorMessage.ReadSheetErr);
                
            }
            return list;
        }

        public override void GenerateCommands(List<TestCommand> commands)
        {
            
            
                if (_exProvider != null)
                {
                    try
                    {
                        int count = 0;
                        foreach (var i in commands)
                        {
                            Automation.Dal.Excel.Model.TestCommand item = new Automation.Dal.Excel.Model.TestCommand()
                            {
                                Target = i.Target,
                                ID = i.ID,
                                Function = i.Function,
                                Keyword = i.Keyword,
                                Object = i.Object
                            };
                            _exProvider.GetSheet<Automation.Dal.Excel.Model.TestCommand>().InsertOnSubmit(item);
                            count++;
                        }
                    }
                    catch (Exception e)
                    {
                        //MessageShow.ExceptionOccur("Generate TCommand sheet exception.", e);
                        log.Error(Resources.ErrorMessage.ReadTCommandSheetErr + e.GetType().ToString() + e.Message);
                        throw new Exception(Resources.ErrorMessage.ReadTCommandSheetErr + e.GetType().ToString());
                    }
                    finally
                    {
                        _exProvider.SubmitChanges();
                    }
                }
                
            
            
        }
    }
}
